﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using DependencyAnalysis;
using DependencyAnalysis.Properties;
using DependencyAnalysis.Serialization;

namespace theeg.DependencyAnalysis.Retrospective
{
    public class ElasticSearchExporter
    {
        private static readonly IList<string> Assemblies = new List<string>
        { 
            typeof(CommonAddin.Properties.IAssemblyIdentifier).Assembly.Location,
            typeof(IAssemblyIdentifier).Assembly.Location,
        };

        public static void Main(string[] args)
        {
            var setup = Setup.BuildStandardSetup();
            setup.StartAnalysis(Assemblies);
            var metricsReport = new MetricsReport { Setup = setup };
            var metrics = (JsonSerializableExpandoObject)metricsReport.FetchReport<JsonSerializableExpandoObject>();
            Console.WriteLine("metrics created");
            var codeSmellReport = new CodeSmellReport { Setup = setup };
            var codeSmell = (JsonSerializableExpandoObject)codeSmellReport.FetchReport<JsonSerializableExpandoObject>();
            Console.WriteLine("smell created");
            var duplicationReport = new DuplicationReport { Setup = setup };
            var duplication = (JsonSerializableExpandoObject)duplicationReport.FetchReport<JsonSerializableExpandoObject>();
            Console.WriteLine("duplication created");

            StoreReport(metrics, "metrics");
            Console.WriteLine("metrics stored");
            StoreReport(codeSmell, "codeSmell");
            Console.WriteLine("smell stored");
            StoreReport(duplication, "duplication");
            Console.WriteLine("duplication stored");
        }

        public static void StoreReport(JsonSerializableExpandoObject report, string typeOfReport)
        {
            try
            {
                var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9200/reports/" + typeOfReport + "/" + DateTime.Now.Ticks);
                webRequest.Method = "PUT";
                webRequest.KeepAlive = false;
                webRequest.Pipelined = false;
                using (var stream = webRequest.GetRequestStream())
                {
                    //var bob = new StringWriter();
                    //var jsonWriter = new JsonWriter(bob);
                    //report.WriteJson(jsonWriter);
                    //Console.WriteLine(bob);

                    var streamWriter = new StreamWriter(stream);
                    var jsonWriter = new JsonWriter(streamWriter);
                    report.WriteJson(jsonWriter);
                    streamWriter.Flush();
                    stream.Close();
                    streamWriter.Close();
                    
                    WebResponse webResponse = webRequest.GetResponse();
                    if (webResponse == null)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

